﻿-- =============================================
-- Author:		顾世杰
-- Create date: 2013.11.11
-- Description:	搜索百度客户
-- =============================================
CREATE PROCEDURE [dbo].[proc_BDCustomer_Search]
	(
		@companyid int,		
		@CusName nvarchar(20),--客户名称或员工名
		@StartIndex int,
		@EndIndex int
	)
AS
BEGIN
	

    ;WITH list as (
	SELECT  
		ROW_NUMBER() OVER (ORDER BY BDServicePersonEname,id DESC) AS Row,
		CustomerId,CustomerName,Project,
	
		SourceId,
		IndustryOneId,
		TypeId,
	
		ProtectId,
		isnull(BDServicePersonEName,'') as BDServicePersonEName
		
	 from customer where companyid=@companyid and audit=1 and delflag=0 and  ProtectId=3 and
	(@CusName='' or BDServicePersonEname = ''+@CusName+'' or CustomerName like '%'+@CusName+'%')	
	)
		
	select *,(select count(1) from list ) as RecordCount,
	
		isnull((Select Title From Customer_Source Where Id=list.SourceId),'...') As Source,
		isnull((Select Title From Customer_Industry Where Id=list.IndustryOneId),'...') As IndustryOne,
		isnull((Select Title From Customer_Type Where Id=list.TypeId),'...') As Type,
	
		ISNULL((Select Title From Customer_Protect Where Id=list.ProtectId),'...') As Protect
	
	
	 from list where row between @StartIndex and @EndIndex order by Row
	
END
